package com.wan.core.timer.task;

import com.wan.core.timer.rule.WRule;
import com.wan.core.timer.rule.WRules;

import java.time.LocalDateTime;
import java.util.concurrent.Executor;

/**
 * 任务构建器
 * @author wwh
 * @date 2022/6/29 14:51
 */
public class TaskBuilder {
    /** 任务 */
    private final WTask task;
    /** 定时规则 */
    private WRule rule;
    /** 执行次数上限, -1代表无限制 */
    private int maxTime;
    /** 间隔周期 */
    private int period;
    /** 执行时间点 */
    private LocalDateTime executeTime;
    /** 线程池 */
    private Executor executor = null;

    private TaskBuilder(WTask task) {
        this.task = task;
    }

    public static TaskBuilder create(WTask task){
        return new TaskBuilder(task);
    }

    public TaskBuilder rule(WRule rule){
        this.rule = rule;
        return this;
    }

    public TaskBuilder maxTime(int max){
        this.maxTime = max;
        return this;
    }

    public TaskBuilder period(int period){
        this.period = period;
        return this;
    }

    public TaskBuilder executeTime(LocalDateTime localDateTime){
        this.executeTime = localDateTime;
        return this;
    }

    public TaskBuilder executor(Executor executor){
        this.executor = executor;
        return this;
    }

    public TaskNode build(){
        if(this.rule == null){
            rule = WRules.ONCE;
        }
        period = Integer.max(1,period);
        if(executeTime == null){
            executeTime = LocalDateTime.now();
        }
        return new TaskNode(rule,maxTime,period,executeTime,task,executor);
    }




}
